package com.kyxx.topics.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.kyxx.topics.BaseApp;
import com.kyxx.topics.R;
import com.kyxx.topics.net.ResponseListener;
import com.kyxx.topics.net.RestClient;
import com.kyxx.topics.net.UrlUtils;
import com.kyxx.topics.utils.TextUtil;
import com.kyxx.topics.utils.Tools;
import com.kyxx.topics.view.MyCommonTitle;
import com.loopj.android.http.RequestParams;

import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;

/**
 * Created by Administrator on 2016/10/7.
 */
public class UserSetPswActivity extends Activity implements View.OnClickListener {
    private MyCommonTitle myCommonTitle;
    private EditText edUserMobile, edPsw;
    private TextView btnSubmit;
    private String mobile, psw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_set_psw);

        initView();
    }

    private void initView() {
        myCommonTitle = (MyCommonTitle) findViewById(R.id.aci_myTitle);
        myCommonTitle.setTitle("设置密码");
        edUserMobile = (EditText) findViewById(R.id.ed_username);//手机号码
        edUserMobile.setText(BaseApp.getModel().getMobile());
        edPsw = (EditText) findViewById(R.id.ed_password);//密码
        btnSubmit = (TextView) findViewById(R.id.btn_submit);//确定

        btnSubmit.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        mobile = edUserMobile.getText().toString().trim();
        psw = edPsw.getText().toString().trim();
        switch (view.getId()) {
            case R.id.btn_submit:
                if (!TextUtil.isMobile(mobile)) {
                    Tools.toast(UserSetPswActivity.this, "手机号码不正确！");
                    return;
                }
                if (!TextUtil.isPasswordLengthLegal(psw)) {
                    Tools.toast(UserSetPswActivity.this, "密码有效长度为6-20位！");
                    return;
                }
                submitData();
                break;
        }
    }

    private void submitData() {
        RequestParams params = new RequestParams();
        params.put("mobile", mobile);
        params.put("Password", psw);
        RestClient.post(UrlUtils.SETPSW, params, UserSetPswActivity.this, new ResponseListener(UserSetPswActivity.this, true) {
            @Override
            public void success(int arg0, Header[] arg1, JSONObject jsonObject) {
                Tools.toast(UserSetPswActivity.this, "密码设置成功！");
                BaseApp.getModel().setPwd(psw);
                finish();
            }

            @Override
            public void failed(int arg0, Header[] arg1, JSONObject jsonObject) {
                super.failed(arg0, arg1, jsonObject);
            }
        });
    }
}
